package cuckoo.aop.config.Listener;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import cuckoo.aop.config.ProxyBeanListener;
import cuckoo.aop.intercept.Interceptor;
import cuckoo.util.ClassUtil;

public class AnnoProxyListener implements ProxyBeanListener {
	private Class<? extends Annotation> annoClass;

	public AnnoProxyListener(Class<? extends Annotation> annoClass) {
		this.annoClass = annoClass;
	}

	@Override
	public Interceptor intercept(Method m) {
		Annotation anno = m.getAnnotation(annoClass);

		if (anno == null) {
			anno = m.getDeclaringClass().getAnnotation(annoClass);
		}

		if (anno == null)
			return null;

		try {
			Class<?> clazz = (Class<?>) annoClass.getMethod("value").invoke(anno);
			return (Interceptor) ClassUtil.instance(clazz);
		} catch (Exception e) {
			return null;
		}
	}
}
